package com.njworkorder.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njworkorder.DTO.RouteAddDto;
import com.njworkorder.Entity.Route;
import com.njworkorder.Entity.Workspace;
import com.njworkorder.Service.RouteService;
import com.njworkorder.Service.WorkspaceService;
import com.njworkorder.Utils.NoCode;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import com.njworkorder.VO.AllRouteLengthInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "路线管理",description = "路线管理相关接口")
@RestController
@RequestMapping("/route")
public class RouteController {

    private final RouteService routeService;
    private final WorkspaceService workspaceService;
    public RouteController(RouteService routeService,WorkspaceService workspaceService) {
        this.routeService = routeService;
        this.workspaceService = workspaceService;
    }

    @Operation(summary = "添加路线信息")
    @PostMapping("/addRoute")
    public Result<String> addRoute(@RequestBody RouteAddDto routeAddDto,
                                   @RequestHeader("Create-Ap-Id") String createApId){

        Route route = new Route();
        BeanUtils.copyProperties(routeAddDto,route);
        route.setCreateApId(createApId);
        route.setRouteNo(NoCode.JSJK + NoCode.LX.name() + routeService.getUpToDateRouteNo());

        boolean save = routeService.save(route);
        if (save){
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.error("添加失败");
    }

    @Operation(summary = "根据id获取路线信息")
    @GetMapping("/getRouteById")
    public Result<Route> getRouteById(@RequestParam("id") String id){
        Route byId = routeService.getById(id);
        return ResultUtil.success(byId);
    }

    @Operation(summary = "根据id删除路线信息")
    @GetMapping("/deleteRouteById")
    public Result<String> deleteRouteById(@RequestParam("id") String id){
        boolean removeById = routeService.removeById(id);
        if (removeById){
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.error("删除失败");
    }

    @Operation(summary = "根据id更新路线信息")
    @PostMapping("/updateRouteById/{id}")
    public Result<String> updateRouteById(@RequestBody RouteAddDto routeAddDto,@PathVariable("id") String id){
        Route route = new Route();
        BeanUtils.copyProperties(routeAddDto,route);
        route.setId(id);

        boolean updateById = routeService.updateById(route);
        if (updateById){
            return ResultUtil.success("更新成功");
        }
        return ResultUtil.error("更新失败");

    }

    @Operation(summary = "获取路线列表")
    @GetMapping("/getList")
    public Result<Page<Route>> getList(@RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize,
                                       @RequestParam(value = "createApId",required = false) String createApId){

        LambdaQueryWrapper<Route> queryWrapper = new LambdaQueryWrapper<Route>()
                .apply("1=1")
                .eq(StringUtils.isNoneEmpty(createApId),Route::getCreateApId,createApId);
        Page<Route> page = routeService.page(new Page<>(pageIndex, pageSize), queryWrapper);
        return ResultUtil.success(page);

    }


    @Operation(summary = "获取所有路线长度等信息")
    @GetMapping("/getAllRouteLengthInfo")
    public Result<AllRouteLengthInfoVo> getAllRouteLengthInfo(){
        LambdaQueryWrapper<Route> queryWrapper = new LambdaQueryWrapper<Route>()
                .eq(Route::getStatus,2);
        List<Route> list = routeService.list(queryWrapper);

        LambdaQueryWrapper<Workspace> queryWrapper1 = new LambdaQueryWrapper<Workspace>()
                .eq(Workspace::getStatus,2);
        long count = workspaceService.count(queryWrapper1);

        int sStake,eStake,totalL = 0;
        String startStake,endStake;
        for (Route route : list){
            if(StringUtils.isNotEmpty(route.getStartStake()) && StringUtils.isNotEmpty(route.getEndStake())){
                startStake = route.getStartStake().replace("K", "").replace("+","");
                endStake = route.getEndStake().replace("K", "").replace("+","");

                sStake = Integer.parseInt(startStake);
                eStake = Integer.parseInt(endStake);

                totalL += eStake - sStake;
            }

        }

        AllRouteLengthInfoVo allRouteLengthInfoVo = new AllRouteLengthInfoVo();
        allRouteLengthInfoVo.setWorkspaceCount((int) count);
        allRouteLengthInfoVo.setRouteCount(list.size());
        allRouteLengthInfoVo.setRouteTotalLength(String.format("%.2f", (double) totalL / 1000));

        return ResultUtil.success(allRouteLengthInfoVo);

    }




}
